//系统类型
var sysType;

$(function () {
    $('#OA').css('display', 'none');
    showModuleTree();
    sysType = "crmSysType";
    $('#saveBtn').css('display', 'none');
    $("#saveForm").css('display', 'none');
});
var setting = {
    view: {
        showLine: true,//显示节点之间的连线。
        selectedMulti: false  //允许同时选中多个节点。
    },
    data: {
        simpleData: {
            enable: true,
            idKey: "id",
            pIdKey: "pId",
            rootPId: 0
        }
    },
    check: {
        enable: true,
        chkStyle: "checkbox",
        chkboxType: { "Y": "ps", "N": "s" }
    }
};
function changeTab(type) {
    if (type == 1) {
        $('.reserve').addClass('active');
        $('.interview').removeClass('active');
        $('#CRM').css('display', 'flex');
        $('#OA').css('display', 'none');
        //将系统类型标识设置为crm系统
        sysType = "crmSysType";
    }
    if (type == 2) {
        $('.interview').addClass('active');
        $('.reserve').removeClass('active');
        $('#OA').css('display', 'flex');
        $('#CRM').css('display', 'none');
        //将系统类型标识设置为oa系统
        sysType = "oaSysType";
    }
}

function editFormFun() {
    var managementTreeObj=$.fn.zTree.getZTreeObj("crmManagementModuleTree");
    var userTreeObj=$.fn.zTree.getZTreeObj("crmUserModuleTree");
    $('#editBtn').css('display', 'none');
    $("#editForm").css('display', 'none');
    $('#saveBtn').css('display', 'block');
    $("#saveForm").css('display', 'block');
    $('#permissionGroupName').prop('disabled', false);
    $('#remark').prop('disabled', false);
    isCheckDisabled(managementTreeObj, false);
    isCheckDisabled(userTreeObj, false);
}

/**
 * 展示添加人员模态框
 * @param row
 */
function showModuleTree() {
    var permissionGroupId = $('#permissionGroupId').val();
    $.ajax({
        type:"post",
        url:"/permissionManagement/getPermissionTree",
        data:{
            "permissionGroupId" : permissionGroupId
        },
        dataType:"json",
        success:function(data){
            if (data.code == '000000') {
                var obj = data.data;
                var zNodesCrmManagement = obj.managementModule;
                $.fn.zTree.init($("#crmManagementModuleTree"), setting, zNodesCrmManagement);
                var managementTreeObj=$.fn.zTree.getZTreeObj("crmManagementModuleTree");
                //expandCheckedNodes(managementTreeObj);
                isCheckDisabled(managementTreeObj, true);
                var zNodesCrmUser = obj.userModule;
                $.fn.zTree.init($("#crmUserModuleTree"), setting, zNodesCrmUser);
                var userTreeObj=$.fn.zTree.getZTreeObj("crmUserModuleTree");
                //expandCheckedNodes(userTreeObj);
                isCheckDisabled(userTreeObj, true);
            } else {
                $.alert({
                    title: '温馨提示',
                    content: '加载失败',
                });
            }
        }
    });
}

function expandCheckedNodes(treeObj, nodes){
    var nodes=treeObj.getCheckedNodes(true);
    if (nodes.length>0) {
        for(var i=0;i<nodes.length;i++){
            var parentNode = nodes[i].getParentNode();
            treeObj.expandNode(parentNode, true, false, false);
        }
    }
}

function isCheckDisabled(treeObj, flag){
    var nodes = treeObj.transformToArray(treeObj.getNodes());
    for (var i=0, l=nodes.length; i < l; i++) {
        treeObj.setChkDisabled(nodes[i], flag);
    }
}

/**
 * 创建权限组提交事件
 */
function addPermissionGroupTab(){
    var createPermissionGroupJson = {};
    var selectCheckedNodesArray = [];//选中节点数据数组
    /** 权限组信息  */
    var permissionGroupMsg = {};
    var permissionGroupName = $('#permissionGroupName').val();//权限组名
    var remark = $('#remark').val();//备注
    permissionGroupMsg.permissionGroupName = permissionGroupName;
    permissionGroupMsg.remark = remark;
    var permissionGroupId = $('#permissionGroupId').val();

    /** 选中节点信息  */
    //系统类型(crm)
    if(sysType == "crmSysType"){
        permissionGroupMsg.sysId = "2";
        createPermissionGroupJson.permissionGroupMsg = permissionGroupMsg;
        //CRM管理模块
        var crmManagerTreeObj = $.fn.zTree.getZTreeObj("crmManagementModuleTree");
        var selectCrmManagerNodes = crmManagerTreeObj.getCheckedNodes(true);
        if(selectCrmManagerNodes.length > 0) {
            for(var index = 0;index < selectCrmManagerNodes.length;index++){
                var selectCheckedNode = {};
                var tmpCheckedNode = selectCrmManagerNodes[index];
                selectCheckedNode.groupId = tmpCheckedNode.groupId;//菜单分组
                selectCheckedNode.nodeId = tmpCheckedNode.id;//节点ID
                selectCheckedNode.menuType = tmpCheckedNode.menuType;//菜单类型(0--一级菜单;1--二级菜单;2--全部地区;3--省份;4--公司)
                //当前节点是否为半选节点 0：一个子节点也没选 1:半选 2：全选
                selectCheckedNode.isCheckAll = tmpCheckedNode.check_Child_State;
                selectCheckedNodesArray.push(selectCheckedNode);
            }
        }

        //CRM用户模块
        var crmUserTreeObj = $.fn.zTree.getZTreeObj("crmUserModuleTree");
        var selectCrmUserNodes = crmUserTreeObj.getCheckedNodes(true);
        if(selectCrmUserNodes.length > 0) {
            for(var index = 0;index < selectCrmUserNodes.length;index++){
                var selectCheckedNode = {};
                var tmpCheckedNode = selectCrmUserNodes[index];
                selectCheckedNode.groupId = tmpCheckedNode.groupId;//菜单分组
                selectCheckedNode.nodeId = tmpCheckedNode.id;//节点ID
                selectCheckedNode.menuType = tmpCheckedNode.menuType;//菜单类型(0--一级菜单;1--二级菜单;2--全部地区;3--省份;4--公司)
                //当前节点是否为半选节点 0：一个子节点也没选 1:半选 2：全选
                selectCheckedNode.isCheckAll = tmpCheckedNode.check_Child_State;
                selectCheckedNodesArray.push(selectCheckedNode);
            }
        }
        createPermissionGroupJson.selectCheckedNodesArray = selectCheckedNodesArray;
    }

    /** 选中节点信息  */
    //系统类型(oa)
    if(sysType == "oaSysType"){

    }

    $.ajax({
        url: "/permissionManagement/addPermissionGroup",
        type: "POST",
        dataType: "json",
        data: {
            "createPermissionGroupJson": JSON.stringify(createPermissionGroupJson),
            "permissionGroupId": permissionGroupId
        },
        async: false,
        success: function (data) {
            if (data.code == '000000') {
                $.alert('操作成功!');
                cancelBtn();
            } else {
                $.alert('操作失败,请稍后再试！');
            }
        },
        error: function () {
        }
    })
    /*
    var managementTreeObj=$.fn.zTree.getZTreeObj("crmManagementModuleTree");
    var userTreeObj=$.fn.zTree.getZTreeObj("crmUserModuleTree");
    $('#editBtn').css('display', 'block');
    $("#editForm").css('display', 'block');
    $('#saveBtn').css('display', 'none');
    $("#saveForm").css('display', 'none');
    $('#permissionGroupName').prop('disabled', true);
    $('#remark').prop('disabled', true);
    isCheckDisabled(managementTreeObj, true);
    isCheckDisabled(userTreeObj, true);
    */
}

function cancelBtn(){
    var $thisTabs = parent.$('.mt-nav-bar .nav-tabs').find('li.active');
    var navTab = parent.$multitabs._getNavTab($thisTabs);
    openTab("权限组名单","/front/permissionManager/permissionList");
    refreshActivePane("/front/permissionManager/permissionList");
    delStorageAndRemove(navTab);
}